package com.da.lon.wang.xlg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.NewPartyLvAdapter;
import com.da.lon.wang.xlg.bean.PartyFragmentBean;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultGroupList;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList.PartyResultProList;
import com.da.lon.wang.xlg.bean.PartyInfoResultBean.PartyInfo;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.ToastUtils;
import com.da.lon.wang.xlg.util.Utils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.osell.selection.widget.MyGridView;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class NewPartyActivity extends BaseActivity implements
        OnRefreshListener<ListView>, OnClickListener {
    protected PartyInfo info;
    ImageButton party_into_top_left, party_info_top_right;
    TextView party_info_title, party_info_rq, party_info_xl, party_info_xp;
    CheckBox party_info_jg;
    ImageView party_info_img;
    MyGridView party_info_gridview;
    ArrayList<PartyResultProList> list;
    private PullToRefreshListView partyLv;
    private ArrayList<PartyResultList> datas;
    private ArrayList<PartyResultGroupList> groupDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_party_info_activity);
        datas = new ArrayList<PartyResultList>();
        groupDatas = new ArrayList<PartyResultGroupList>();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        initView();
        getDatas(id);

    }

    /**
     * 控件初始化
     */
    private void initView() {
        party_into_top_left = (ImageButton) findViewById(R.id.party_into_top_left);
        party_info_top_right = (ImageButton) findViewById(R.id.party_info_top_right);
        party_info_title = (TextView) findViewById(R.id.party_info_title);
        partyLv = (PullToRefreshListView) findViewById(R.id.ocoffee_newparty_lv);
        partyLv.setMode(Mode.DISABLED);
        partyLv.setOnRefreshListener(this);
        party_into_top_left.setOnClickListener(this);
        party_info_top_right.setOnClickListener(this);
    }

    private void getDatas(String activity_group_id) {
        try {
            String url = XLGConstants.URL + XLGConstants.URL_PARTY_HEAD
                    + "groupList";
            HttpLoader<PartyFragmentBean> loader = new HttpLoader<PartyFragmentBean>(
                    this, url, PartyFragmentBean.class);
            AjaxParams params = new AjaxParams();

            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Activity" + XLGConstants.SECRET
                            + "groupList"));
            params.put("activity_group_id", activity_group_id);
            loader.loadData(params);
            loader.setManagerListener(new OnWebLoadListener<PartyFragmentBean>() {

                private NewPartyLvAdapter adapter;

                @Override
                public void OnStart() {
                    showDialog();

                }

                @Override
                public void OnError(String error) {
                    dismissDialog();
                }

                @Override
                public void onSuccess(PartyFragmentBean t) {
                    dismissDialog();
                    if (t != null && "000000".equals(t.getCode())
                            && t.getResult() != null) {
                        info = t.getResult().getInfo();
                        datas = t.getResult().getActivity_list();
                        groupDatas = t.getResult().getActivitys_group_list();
                        if (info.getName() != null) {
                            party_info_title.setText(info.getName());
                        }
                        if (datas != null) {
                            adapter = new NewPartyLvAdapter(
                                    NewPartyActivity.this, datas, groupDatas);
                            partyLv.setAdapter(adapter);
                        }
                    } else {
                        ToastUtils.showShort(NewPartyActivity.this,
                                t.getMessage());
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.party_into_top_left:
                finish();
                break;
            case R.id.party_info_top_right:
                // 分享
                if (null != info) {
                    Utils.showShare(NewPartyActivity.this, info.getName(), info.getInfo(),
                            info.getShare_url(), info.getShare_img());
                }
                break;

        }
    }
}
